package com.eric.code.oauth2.user;

import com.eric.code.oauth2.common.BaseService;
import com.eric.code.oauth2.model.User;
import com.jfinal.kit.HashKit;
import com.jfinal.plugin.activerecord.Page;

import java.util.Date;

/**
 * Created by eric on 17-3-23.
 */
public class UserService extends BaseService{

    public static final UserService me = new UserService();
    private final User dao = new User().dao();

    public Page<User> paginate(int pageNumber, int pageSize) {
        return dao.paginate(pageNumber, pageSize, "select * ", "from oauth2_user where utype=0  order by createAt desc");
    }

    public Page<User> paginate(int pageNumber) {
        return dao.paginate(pageNumber, PAGE_SIZE, "select * ", "from oauth2_user where utype=0 order by createAt desc");
    }

    /**
     * 初始化默认密码 123456
     * @param user
     */
    public void save(User user){
        user.setCreateAt(new Date());
        user.setPassword(HashKit.md5("123456"));
        user.setUserName(user.getEmail());
        user.save();
    }

    public User findById(long userId){
        return dao.findById(userId);
    }

    /**
     * 修改用户信息
     * @param user
     */
    public void update(User user){
        user.update();
    }

    public void delete(long uid){
        dao.deleteById(uid);
    }
}
